package com.wyy.travels.controller;/*
 **
 * @author yangyang
 *@date 2022/07/19 20:33
 **/

import com.wyy.travels.entity.Result;
import com.wyy.travels.entity.User;
import com.wyy.travels.service.impl.UserDaoService;
import com.wyy.travels.utils.CreateImageCode;
import com.wyy.travels.utils.ValidateImageCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("user")
@Slf4j
@CrossOrigin
public class UserController {
    @Autowired
    private UserDaoService  userDaoService;

    @GetMapping("getImage")
    public Map<String,String> getImage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String,String> result=new HashMap<>();
        CreateImageCode createImageCode=new CreateImageCode();
        String securityCode = createImageCode.getCode();
        System.out.println("========验证码====="+securityCode);
        String format = new SimpleDateFormat("yyyyMMddHH:MM:ss").format(new Date());
        request.getServletContext().setAttribute(format, securityCode);
        ByteArrayOutputStream bos=new ByteArrayOutputStream();
        BufferedImage image = createImageCode.getBuffImg();
        ImageIO.write(image,"png", bos);
        String encode = Base64Utils.encodeToString(bos.toByteArray());
        result.put("key", format);
        result.put("image", encode);
        return  result;
    }

     @PostMapping("register")
    public Result  register(String code,String key, @RequestBody User user, HttpServletRequest request){
         Result result=new Result();
         String sessionCode = (String) request.getServletContext().getAttribute(key);
         System.out.println(code+"=====register======"+sessionCode);
         try {
             if (sessionCode.equalsIgnoreCase(code)){
                 userDaoService.register(user);
                 result.setMsg("注册成功");
             }else {
                 throw  new RuntimeException("验证码错误");
             }
         }catch (Exception e){
             e.getStackTrace();
             return  result.setMsg(e.getMessage()).setState(false);

         }
         return  result;
     }

     @PostMapping("login")
     public  Result login(@RequestBody User user,HttpServletRequest request){
         Result result=new Result();
         log.info("=========="+user);
         try {
             User login = userDaoService.login(user);
             log.info("====login======="+login.getId());
             request.getServletContext().setAttribute(login.getId(),login);
             result.setMsg("登录成功");
         }catch (Exception e){
             result.setState(false).setMsg(e.getMessage());
         }
         return  result;

     }





}
